package com.securinets;


import java.util.ArrayList;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;


import com.securinets.BD.ArticlesDBAdapter;
import com.securinets.events.EventsActivity;
import com.securinets.json.ClientJSON;
import com.securinets.news.Article;
import com.securinets.news.NewsActivity;
import com.securinets.securiday.SecuridayActivity;

public class Main extends TabActivity {
	 /** Called when the activity is first created. */
		private final static int MENU_SYNC = 1;
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec; // reusable tabspec for each tab
        Intent intent;
     
        intent = new Intent().setClass(this, NewsActivity.class);
        spec = tabHost.newTabSpec("news")
                .setIndicator(res.getText(R.string.Onglet1))
     
                .setContent(intent);
        tabHost.addTab(spec);
     
        intent = new Intent().setClass(this, SecuridayActivity.class);
        spec = tabHost.newTabSpec("securiday")
                        .setIndicator(res.getText(R.string.Onglet2))
                        .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, EventsActivity.class);
        spec = tabHost.newTabSpec("events")
                .setIndicator(res.getText(R.string.Onglet3))
     
                .setContent(intent);
        tabHost.addTab(spec);
     
//        intent = new Intent().setClass(this, ContactAcivity.class);
//        spec = tabHost.newTabSpec("contact")
//                        .setIndicator(res.getText(R.string.Onglet4))
//                        .setContent(intent);
//        tabHost.addTab(spec);
     
        tabHost.setCurrentTab(0);
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, MENU_SYNC, Menu.NONE, "Mise à jour");
		return true;
		
	}
	
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_SYNC:
			
			new Thread(new Runnable() {

		        @Override
		        public void run() {
		        	  try {
		        		  
		           	 ArrayList<Article> articles = ClientJSON.resultatsSecurinetsJSON("/android.php");
		           	 ArticlesDBAdapter DB = new ArticlesDBAdapter(Main.this);
		           	 DB.open();
		           	 DB.insertArticles(articles);
		           	 DB.close();
		           	 
		           	 
		        	  } catch (Throwable t) {Log.i("ClientJSON", t.toString()); }
		        }
		    }).start();
			
		//	Log.i("ClientJSON", "Terminé");
			
			
			return true;

		default:
			return true;
		}
		
		
	}
	
	
    
    
}
